<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-10-05
 * Time: 22:37
 */

namespace app\service;


use app\model\Supplier;
use app\validate\SupplierValidate;
use think\facade\Request;

class SupplierService
{
    //分页
    public function page()
    {
        //获取request参数
        $request = Request::get();
        $name = isset($request['name']) ? $request['name'] : '';
        $status = isset($request['status']) ? $request['status'] : -1;
        //封装where查询条件
        $where = [];
        $status == -1   ||  $where[]   = ['status', '=' ,$status];
        empty($name)    ||  $where[]   = ['name', 'like', '%'.$name.'%'];
        //拼接page参数
        $pageParam = ['query' => []];
        $pageParam['query']['status'] = $status;
        $pageParam['query']['name'] = $name;

        return Supplier::where($where)->order('id desc')->paginate(10, false, $pageParam);
    }

    //统计个数
    public function count()
    {
        return Supplier::count();
    }

    //保存数据
    public function save()
    {
        Request::isPost() || die('request not post!');
        //获取参数
        $param = Request::param();
        //是否通过验证
        $error = $this->_validate($param);

        if(is_null($error)){
            //添加数据
            $supplier = new Supplier();
            //检测错误
            //allowField(true)过虑post数组中非数据表字段数据
            if( $supplier->allowField(true)->save($param) ){
                return ajax_return(0, '保存成功');
            }else{
                return ajax_return(100, '保存失败');
            }
        }else{
            return ajax_return(100, $error);
        }
    }

    //根据id得到数据
    public function edit($id)
    {
        return Supplier::get($id);
    }

    public function update()
    {
        Request::isPost() || die('request not post!');
        //获取参数
        $param = Request::param();
        //是否通过验证
        $error = $this->_validate($param);

        if(is_null($error)){
            $supplier = Supplier::get($param['id']);
            $supplier->name 	= $param['name'];
            $supplier->contact 	= $param['contact'];
            $supplier->phone 	= $param['phone'];
            $supplier->email 	= $param['email'];
            $supplier->fax 		= $param['fax'];
            $supplier->address 	= $param['address'];
            $supplier->status 	= $param['status'];
            $supplier->desc 	= $param['desc'];

            // 检测错误
            if( $supplier->save() ){
                return ajax_return(0, '修改成功');
            }else{
                return ajax_return(100, '修改失败');
            }
        }else{
            return ajax_return(100,$error);
        }
    }

    public function delete($id)
    {
        Request::isPost() || die('request not post!');

        if(Supplier::destroy($id)){
            return ajax_return(0, '删除成功');
        }else{
            return ajax_return(100, '删除失败');
        }
    }

    //验证器
    private function _validate($data)
    {
        //验证
        $validate = new SupplierValidate();
        if(!$validate->check($data))
        {
            return $validate->getError();
        }
    }
}